package com.material.controller.employee;


import com.material.common.annotation.LoginUser;
import com.material.common.core.domain.entity.SysUser;
import com.material.common.entity.ResultResponse;
import com.material.factory.domain.FactoryInfo;
import com.material.factory.service.IFactoryInfoService;
import com.material.request.EmployeeUserRequest;
import com.material.response.EmployeeInfoResponse;
import com.material.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @author ganxinge
 */
@Api(tags = "员工信息")
@RestController
@RequestMapping("/employee/info")
public class EmployeeUserInfoController {

    @Autowired
    private IFactoryInfoService factoryInfoService;
    @Autowired
    private ISysUserService sysUserService;

    @ApiOperation("员工信息")
    @GetMapping("/userDetail")
    public ResultResponse userDetail(@ApiIgnore @LoginUser SysUser sysUser){
        Long factoryId = sysUser.getBizId();
        FactoryInfo factoryInfo = factoryInfoService.selectFactoryInfoById(factoryId);
        SysUser user = sysUserService.selectUserById(sysUser.getUserId());
        EmployeeInfoResponse response = new EmployeeInfoResponse();
        response.setFactoryName(factoryInfo.getName());
        response.setLoginName(user.getUserName());
        response.setUserName(user.getNickName());
        response.setSex(user.getSex());
        response.setAvatar(user.getAvatar());
        return ResultResponse.success(response);
    }

    @ApiOperation("保存员工信息")
    @PostMapping("saveUserInfo")
    public ResultResponse saveUserInfo(@ApiIgnore @LoginUser SysUser sysUser,@RequestBody EmployeeUserRequest employeeUserRequest){
        String sex = employeeUserRequest.getSex();
        String userName = employeeUserRequest.getUserName();
        Long userId = sysUser.getUserId();

        SysUser update = new SysUser();
        update.setUserId(userId);
        update.setNickName(userName);
        update.setSex(sex);
        sysUserService.updateUserProfile(update);
        return ResultResponse.success();
    }
}
